﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Golos.Polls.PollCache
{
    [Serializable]
    public class PollInfo
    {
        public Guid Id { get; private set; }
        public Guid OwnerUserId { get; private set; }
        public string QuestionText { get; private set; }
        public DateTime? EndDate { get; private set; }
        public Dictionary<Guid, Answer> Answers { get; private set; }
        public bool IsDeleted { get; private set; }

        public PollInfo(Guid id, Guid ownerUserId, DateTime? endDate, string questionText, bool isDeleted)
        {

            Id = id;
            OwnerUserId = ownerUserId;
            QuestionText = questionText;
            EndDate = endDate;
            Answers = new Dictionary<Guid, Answer>();
            IsDeleted = isDeleted;
        }

        public PollInfo(Guid ownerUserId, string questionText, List<Answer> answers)
        {
            Id = Guid.NewGuid();
            OwnerUserId = ownerUserId;
            QuestionText = questionText;
            EndDate = null;
            Answers = new Dictionary<Guid, Answer>();
            foreach (Answer answer in answers)
            {
                Answers.Add(answer.Id, answer);
            }
        }

        public PollInfo(Guid ownerUserId, DateTime endDate, string questionText, List<Answer> answers)
        {
            Id = Guid.NewGuid();
            OwnerUserId = ownerUserId;
            QuestionText = questionText;
            EndDate = endDate;
            Answers = new Dictionary<Guid, Answer>();
            foreach (Answer answer in answers)
            {
                Answers.Add(answer.Id, answer);
            }
        }

        public void AddAnswer(Answer answer)
        {
            lock (Answers)
            {
                if (!Answers.ContainsKey(answer.Id))
                    Answers.Add(answer.Id, answer);
                else
                    Answers[answer.Id] = answer;
            }
        }

        public void Delete()
        {
            IsDeleted = true;
        }

        public void SetOwner(Guid newOwnerUserId)
        {
            OwnerUserId = newOwnerUserId;
        }

        public byte[] Serialize()
        {
            MemoryStream stream = new MemoryStream();
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, this);
            stream.Close();
            return stream.ToArray();
        }

        public static PollInfo Deserialize(Stream stream)
        {
            BinaryFormatter bFormatter = new BinaryFormatter();
            PollInfo pollInfo = (PollInfo)bFormatter.Deserialize(stream);
            stream.Close();
            return pollInfo;
        }
    }

    [Serializable]
    public class Answer
    {
        public Guid Id { get; private set; }
        public int OrderNumber { get; private set; }
        public string AnswerText { get; private set; }

        public Answer(Guid id, string answerText, int orderNumber)
        {
            Id = id;
            AnswerText = answerText;
            OrderNumber = orderNumber;
        }
    }
}
